from enum import Enum

class IntentionType(Enum):
    """
    意图类型枚举 - 用于Langgraph工作流中的意图识别
    替换硬编码字符串，提高代码可维护性
    """
    # 基础意图
    HELP = "HELP"  # 帮助
    BAVARDER = "BAVARDER"  # 闲聊
    KUMQUAT_INFO = "KUMQUAT_INFO"  # 金桔信息
    EMPTY_INSTRUCTION = "EMPTY_INSTRUCTION"  # 空指令
    AI_UNAVAILABLE = "AI_UNAVAILABLE"  # AI不可用
    
    # 用户管理
    MANAGE_USER = "MANAGE_USER"  # 用户管理
    
    # 订单管理
    MANAGE_MY_ORDER = "MANAGE_MY_ORDER"  # 管理个人订单
    REPORT_TODAY = "REPORT_TODAY"  # 今日汇报
    REPORT_ORDERS = "REPORT_ORDERS"  # 订单报告
    CHECK_MY_ORDERS = "CHECK_MY_ORDERS"  # 查看个人订单
    
    # 金桔管理
    CHECK_MY_KUMQUATS = "CHECK_MY_KUMQUATS"  # 查看个人金桔
    KUMQUAT_MANAGE = "KUMQUAT_MANAGE"  # 金桔管理
    
    def __str__(self):
        return self.value


class UserRole(Enum):
    """
    用户角色枚举 - 用于用户权限管理
    替换硬编码字符串，提高代码可维护性
    """
    USER = "USER"  # 普通用户
    ADMIN = "ADMIN"  # 管理员
    MANAGER = "MANAGER"  # 经理
    ANONYMOUS = "ANONYMOUS"  # 匿名用户
    
    def __str__(self):
        return self.value
    
    @classmethod
    def get_all_roles(cls):
        """获取所有角色列表"""
        return [role.value for role in cls]
    
    @classmethod
    def get_upgrade_roles(cls):
        """获取可升级的角色列表（不包括ANONYMOUS）"""
        return [role.value for role in cls if role != cls.ANONYMOUS]


class ActionType(Enum):
    """
    动作类型枚举 - 用于chat_handler.action记录
    替换硬编码字符串，提高代码可维护性
    """
    # 工作流级别动作
    WORKFLOW_START = "WORKFLOW_START"
    WORKFLOW_COMPLETE = "WORKFLOW_COMPLETE"
    WORKFLOW_SUCCESS = "WORKFLOW_SUCCESS"
    WORKFLOW_ERROR = "WORKFLOW_ERROR"
    
    # 处理级别动作
    START_PROCESSING = "START_PROCESSING"
    PROCESSING_ERROR = "PROCESSING_ERROR"
    UNSUPPORTED_MESSAGE_TYPE = "UNSUPPORTED_MESSAGE_TYPE"
    
    # 意图识别动作
    INTENTION_START = "INTENTION_START"
    INTENTION_RECOGNIZED = "INTENTION_RECOGNIZED"  # 需要与具体意图组合使用
    INTENTION_ERROR = "INTENTION_ERROR"
    EMPTY_MESSAGE = "EMPTY_MESSAGE"
    
    # 权限校验动作
    PERMISSION_START = "PERMISSION_START"
    PERMISSION_GRANTED = "PERMISSION_GRANTED"
    PERMISSION_DENIED = "PERMISSION_DENIED"
    PERMISSION_ERROR = "PERMISSION_ERROR"
    
    # 业务逻辑动作
    BUSINESS_START = "BUSINESS_START"  # 需要与具体意图组合使用
    BUSINESS_COMPLETE = "BUSINESS_COMPLETE"  # 需要与具体意图组合使用
    BUSINESS_ERROR = "BUSINESS_ERROR"
    
    def __str__(self):
        return self.value
    
    @classmethod
    def with_context(cls, action_type, context: str = None):
        """
        为需要上下文的动作类型添加上下文信息
        
        Args:
            action_type: 动作类型
            context: 上下文信息（如意图类型）
            
        Returns:
            带上下文的动作字符串
        """
        if context:
            return f"{action_type.value}:{context}"
        return action_type.value


def get_inst_by_name(instruction_name):
    try:
        inst = InstructionType[instruction_name]
        return inst
    except KeyError:
        return None


def get_intention_by_name(intention_name):
    """
    根据名称获取意图类型枚举
    
    Args:
        intention_name: 意图名称字符串
        
    Returns:
        IntentionType枚举或None
    """
    try:
        return IntentionType(intention_name)
    except ValueError:
        return None


def get_action_by_name(action_name):
    """
    根据名称获取动作类型枚举
    
    Args:
        action_name: 动作名称字符串
        
    Returns:
        ActionType枚举或None
    """
    try:
        return ActionType(action_name)
    except ValueError:
        return None

